//添加新用户
function newUsers() {
    //添加用户对话框
    $('#adddg').dialog('open').dialog('setTitle', '添加用户');
    //数据清空
    $('#fam').form('clear');
    $('#sex').combobox('select', 1);

}

//用户信息修改
function editUsers() {
    //选中某一行
    var row = $('#datagrid').datagrid('getSelected');
    var rows = $('#datagrid').datagrid('getSelections');
    if (rows && rows.length != 1) {
        myAlert('系统警告！', '请勾选一个选项');
        return
    }
    if (row) {
        $('#modifydg').dialog('open').dialog('setTitle', '修改信息');
        //显示未修改前的用户信息
        $('#fim').form('load', row);
    }
}

//用户信息添加按钮事件
function addUsers() {
    var add = "/user/add";
    $('#fam').form('submit', {
        url: add,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var result = eval('(' + result + ')');
            if (result.status == 0) {

                $('#adddg').dialog('close');		// close the dialog
                $('#datagrid').datagrid('reload');	// reload the user data
                $.messager.show({
                    title: 'Success',
                    msg: '添加成功'
                });
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result.msg
                });
            }
        }
    });
}

//修改用户信息
function updateUsers() {
    var row = $('#datagrid').datagrid('getSelected');
    var add = "/user/update?id=" + row.id;
    $('#fim').form('submit', {
        url: add,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var result = eval('(' + result + ')');
            if (result.status == 0) {

                $('#modifydg').dialog('close');		// close the dialog
                $('#datagrid').datagrid('reload');	// reload the user data
                $.messager.show({
                    title: 'Success',
                    msg: '保存成功'
                });
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result.msg
                });
            }
        }
    });
}

//用户删除按钮事件
function removeUsers() {
    var row = $('#datagrid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('Confirm', '确定要删除该用户?', function (r) {
            if (r) {
                $.post('delete', {id: row.id}, function (result) {
                    if (result.status == 0) {
                        $('#datagrid').datagrid('reload');	// reload the user data
                        $.messager.show({
                            title: 'Success',
                            msg: '删除成功'
                        });
                    } else {
                        $.messager.show({	// show error message
                            title: 'Error',
                            msg: result.msg
                        });
                    }
                }, 'json');
            }
        });
    }
}